import time

import pyautogui

imgpath = "./2560_1440/"
count = 0
classname = '船舶'


def if_png_exist(name):  # 没啥用的函数
    location = pyautogui.locateOnScreen(image=imgpath + name + '.png')
    if location is None:
        return 0
    else:
        return 1


def refresh():  # 检测选课页面是否刷新出来
    location = pyautogui.locateOnScreen(image=imgpath + '刷新1.png',region=(600,150, 200, 100))
    #print(location)
    #Box(left=637, top=207, width=51, height=13)
    if location is not None:
        print('页面已刷新')
        return 1
    else:
        location = pyautogui.locateOnScreen(image=imgpath + '刷新2.png', region=(600, 150, 200, 100))
        #print(location)
        #Box(left=654, top=207, width=50, height=13)
        if location is not None:
            print('页面已刷新')
            return 1
        else:
            print('页面未刷新')
            return -1


def noclass():  # 检测是否有课
    location = pyautogui.locateOnScreen(image=imgpath + '无课.png',region=(500, 200, 300, 100))
    #print(location)
    #Box(left=544, top=233, width=169, height=13)
    if location is None:
        return -1
    else:
        while True:
            pyautogui.moveTo(50, 50)
            location = pyautogui.locateOnScreen(image=imgpath + '查询.png',region=(1000, 100, 300, 100))
            if location is not None:
                print(location)
                #Box(left=1087, top=158, width=28, height=20)
                x, y = pyautogui.center(location)
                pyautogui.click(x=x, y=y, clicks=1, button='left')
                return 1


def success():  # 检测选课是否成功
    # time.sleep()
    location = pyautogui.locateOnScreen(image=imgpath + '成功.png')
    if location is not None:
        confirm()
        return 1
    else:
        return -1


def full():  # 检测人数是否已满
    # time.sleep()
    location = pyautogui.locateOnScreen(image=imgpath + '满员.png',region=(400,100, 300, 100))
    #print(location)
    #Box(left=431, top=121, width=188, height=15)
    if location is not None:
        confirm()
        return 1
    else:
        return -1

def confirm():
    while True:
        #pyautogui.moveTo(50, 50)
        location = pyautogui.locateOnScreen(image=imgpath + '确定.png',region=(600,100, 300, 200))
        time.sleep(0.15)
        if location is not None:
            #print(location)
            #Box(left=727, top=159, width=33, height=20)
            x, y = pyautogui.center(location)
            pyautogui.click(x=x, y=y, clicks=1, button='left')
            return 1

def search():
    location = pyautogui.locateOnScreen(image=imgpath + '查询.png', region=(1000, 100, 300, 100))
    if location is not None:
        print(location)
        # Box(left=1087, top=158, width=28, height=20)
        x, y = pyautogui.center(location)
        pyautogui.click(x=x, y=y, clicks=1, button='left')

'''
a = pyautogui.locateCenterOnScreen(image='网络2.png')
# x, y = pyautogui.center(location)
print(a)
'''


def run():
    print('run()启动')
    search()
    while refresh() == -1:
        pass
        #time.sleep(0.1)
    if noclass() == 1:
        print('暂无课程，刷新中')
        return -1
    print('搜索课程')
    location = pyautogui.locateOnScreen(image=imgpath + classname + '.png',region=(100,300, 100, 100))
    #print(location)
    #Box(left=164, top=361, width=25, height=12)
    if location is not None:  # 如果课程存在

        x1, y1 = pyautogui.center(location)
        print('已找到课程' + str(location))
        color_location = pyautogui.locateOnScreen(image=imgpath + '选课1.png',region=(1100,200, 100, 100))
        #print(color_location)
        #Box(left=1155, top=245, width=25, height=13)
        x3, y3 = pyautogui.center(color_location)
        #im = pyautogui.screenshot()

        # 调试选课背景
        print(x1, y1, x3, y3)
        print(x1 - 30, y1, x3 - 30, y3)
        #print(pyautogui.pixel(x3 - 30, y3))
        #print(pyautogui.pixel(x1 - 30, y1))
        # print(im.getpixel((x3 - 30, y3)))
        # print(im.getpixel((x1 - 30, y1)))

        # 旧的判断代码 pixelMatchesColor函数不稳定经常出错
        # color_choose = pyautogui.pixelMatchesColor(x1 - 30, y1, im.getpixel((x3 - 30, y3)))
        # print(color_choose)
        for i in range(3):
            try:
                color_choose = pyautogui.pixelMatchesColor(x1 - 30, y1, pyautogui.pixel(x3 - 30, y3))
                print(color_choose)
                break
            except:
                print('颜色判断错误第' + str(i) + '次')
                if i==2:
                    print('事不过三！ tmd这pixelMatchesColor函数怎么总出错 f**k')
                    return -1

        # if (pyautogui.pixel(x3 - 30, y3).red==pyautogui.pixel(x1 - 30, y1).red)and (pyautogui.pixel(x3 - 30, y3).green==pyautogui.pixel(x1 - 30, y1).green) and (pyautogui.pixel(x3 - 30, y3).blue==pyautogui.pixel(x1 - 30, y1).blue):
        if color_choose:
            select = pyautogui.locateAllOnScreen(image=imgpath + '选课1.png',region=(1100,200, 100, 1000))
            print('蓝色背景')
        else:
            select = pyautogui.locateAllOnScreen(image=imgpath + '选课2.png',region=(1100,200, 100, 1000))
            print('白色背景')

        for i in select:  # 选择对应的选课按钮
            #print(i)
            #Box(left=1155, top=245, width=25, height=13)
            x2, y2 = pyautogui.center(i)
            if (y1 - 10) < y2 < (y1 + 10):
                pyautogui.click(x=x2, y=y2, clicks=1, button='left')
                confirm()  # 等待确认
                while True:
                    if success() == 1:
                        print('选课成功')
                        return 1
                    elif full() == 1:
                        print('所选课程已满')
                        return -1



    elif location is None:
        print('未找到' + classname + '课程')
        return -1


while count <= 5:
    if run() == 1:
        print('选课成功，继续搜索')
        count += 1
    else:
        print('选课失败，继续搜索')
    #time.sleep(0.1)
    print('run()执行完毕\n')
